﻿//-----------------------------------------------------------------------
// <copyright file="Log.cs" company="CKS.Office365">
// CKS Office365 CodePlex
// </copyright>
//-----------------------------------------------------------------------

namespace CKS.Office365.Diagnostics
{
    using System;
    using System.Globalization;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;

    /// <summary>
    /// Defines the level of logging for CKS.Office365
    /// </summary>
    public enum LoggingLevel
    {
        /// <summary>
        /// Indicates a problem, but does not need immediate investigation
        /// </summary>
        Monitorable,

        /// <summary>
        /// Non critical information for the administrator
        /// </summary>
        Information,

        /// <summary>
        /// A forewarning of potential problems
        /// </summary>
        Warning,

        /// <summary>
        /// To help support or test teams debug customer or environmental issues
        /// </summary>
        Medium,

        /// <summary>
        /// Events that require the attention of the system administrator
        /// </summary>
        High,

        /// <summary>
        /// Events that demand the immediate attention of the system administrator
        /// </summary>
        Critical
    }

    /// <summary>
    /// Provides a facility to write logging information to a CKS.Office365 logging list 
    /// </summary>
    public class Log
    {
        /// <summary>
        /// Local instance of the logging list
        /// </summary>
        private SPList logging;

        /// <summary>
        /// Initializes a new instance of the <see cref="Log"/> class.
        /// </summary>
        /// <param name="siteContext">The site context.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public Log(SPSite siteContext)
        {
            if (siteContext == null)
            {
                throw new ArgumentNullException("siteContext");
            }

            this.logging = siteContext.RootWeb.Lists.TryGetList(GlobalConstants.DefaultLoggingListName);

            if (this.logging == null)
            {
                throw new SPException("The CKS.Office365 Logging list could not be found.");
            }
        }

        /// <summary>
        /// Writes information to the Office365 log.
        /// </summary>
        /// <param name="webContext">The web context.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public static void QuickLogInformational(SPWeb webContext, string message, params object[] args)
        {
            if (webContext == null)
            {
                throw new ArgumentNullException("webContext");
            }

            if (args != null)
            {
                message = string.Format(CultureInfo.InvariantCulture, message, args);
            }

            Log log = new Log(webContext.Site);
            log.Write(message, string.Empty, string.Empty, LoggingLevel.Information);
        }

        /// <summary>
        /// Writes a message to the logging list.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="description">The description.</param>
        /// <param name="category">The category.</param>
        /// <param name="level">The level.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public void Write(string title, string description, string category, string level)
        {
            SPListItem item = this.logging.AddItem();
            item["Title"] = title;
            item["CKSMessageDescription"] = description;
            item["CKSMessageCategory"] = category;
            item["CKSMessageLevel"] = level;
            item.Update();
        }

        /// <summary>
        /// Writes a message to the logging list.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="description">The description.</param>
        /// <param name="category">The category.</param>
        /// <param name="level">The level.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public void Write(string title, string description, string category, LoggingLevel level)
        {
            SPListItem item = this.logging.AddItem();
            item["Title"] = title;
            item["CKSMessageDescription"] = description;
            item["CKSMessageCategory"] = category;
            item["CKSMessageLevel"] = level.ToString();
            item.Update();
        }
    }
}
